package com.jiandaoshou;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.LoginInfo;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.JsonUtil;
import com.jiandaoshou.utils.ToastUtil;
import com.jiandaoshou.utils.UserUtils;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2016/10/17.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private EditText etPwd, edt_account;
    private boolean isShow;
    private ImageView ivShow;
    private TextView tv_login;

    @Override
    public boolean showBackLogo() {
        return false;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.layout_login;
    }

    @Override
    protected String getTitleText() {
        return null;
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        starter.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(starter);
    }

    @Override
    public void init() {
        edt_account = $(R.id.edt_account);
        etPwd = $(R.id.edt_pwd);
        ivShow = $(R.id.iv_show);
        tv_login = $(R.id.tv_login);
        tv_login.setOnClickListener(this);
        ivShow.setOnClickListener(this);
    }

    @Override
    public boolean isShowTitle() {
        return false;
    }

    private void changePwdShow() {
        if (isShow) {
            etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            isShow = false;
            ivShow.setImageResource(R.mipmap.icon_eye);
        } else {
            etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            isShow = true;
            ivShow.setImageResource(R.mipmap.icon_eye_show);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login:
                login();
                break;
            case R.id.iv_show:
                changePwdShow();
                break;
        }
    }

    private void login() {
        String account = edt_account.getText().toString().trim();
        String pwd = etPwd.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            ToastUtil.toast(getString(R.string.input_account));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.toast(getString(R.string.input_pwd));
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("strNumber", account);
        map.put("strPassword", pwd);
        Http.post(APIDefine.TF_Interface_IFUser_LoginIn, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) {
                LoginInfo info = JsonUtil.parser.fromJson(response, LoginInfo.class);
                if(info.getAllowLogin()==1){
                    String strid = info.getShopInfoJson().getStrID();
                    if(!TextUtils.isEmpty(strid)){
                        if (strid.contains("-")) {
                            JPushInterface.setAlias(LoginActivity.this, strid.replaceAll("-", "_"), null);
                        } else {
                            JPushInterface.setAlias(LoginActivity.this, strid, null);

                        }
                    }
                    UserUtils.saveUser(context, info.getShopInfoJson());
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    intent.putExtra("shopId",info.getShopInfoJson().getStrID());
                    startActivity(intent);
                    finish();
                }else{
                    ToastUtil.toast("登陆失败！");
                }
            }

            @Override
            public void onHttpError(String error) {
                super.onHttpError(error);
            }
        });
    }
}
